﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Engine.Sgml.Parser {
    internal class StringReader : TextReader {
        private StringBuilder stringBuilder;
        private int position;

        public StringReader(StringBuilder builder) {
            Guard.ArgumentNotNull(builder, "builder");
            this.stringBuilder = builder;
            }

        public override int Read() {
            if (position < stringBuilder.Length)
                return stringBuilder[position++];
            else
                return SgmlStream.EndOfFile;
            }

        public override int Peek() {
            if (position < stringBuilder.Length)
                return stringBuilder[position];
            else
                return SgmlStream.EndOfFile;
            }

        public int Position {
            get { return position; }
            set {
                if (value < 0 || stringBuilder.Length <= value)
                    throw new ArgumentOutOfRangeException();
                this.position = value;
                }
            }

        public override void Close() {
            stringBuilder.Clear();
            base.Close();
            }
        }
    }
